package com.leesche.zcsp.node.net.timer;

import com.leesche.zcsp.node.net.service.data.NetDevice;
import com.leesche.zcsp.node.web.model.PubDevice;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDeviceDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDeviceCondition;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by denniesyang on 2016-10-31.
 */
@Component
@Lazy(value = false)
public class DeviceTimer {
    private final static Logger LOGGER = Logger.getLogger(DeviceTimer.class);
    /***
     * 设备名 ：设备
     */
    public static Map<String, NetDevice> deviceMap = new HashMap<String, NetDevice>();
//    public static Map<byte[], byte[]> deviceMap = new HashMap<byte[], byte[]>();


    @Autowired
    PubDeviceService pubDeviceService;

  /*  private static   DeviceTimer deviceTimer;

    public static  DeviceTimer getIntance(){
        if(deviceTimer!=null){
            return deviceTimer;
        }else{
            return new DeviceTimer();
        }

    }*/

    /**
     * <设备加载>
     */
   @Scheduled(fixedRate = 1000 * 60 * 60 * 2)
    public void doDeviceTimer() {
        LOGGER.info("开始加载并缓存所有设备信息......");
        QueryPubDeviceCondition condition = new QueryPubDeviceCondition();
        List<PubDeviceDto> lists = pubDeviceService.findListsByCondition(condition);
        for (PubDeviceDto pubDeviceDto : lists) {
            if (!deviceMap.containsKey(pubDeviceDto.getImme())) {
                deviceMap.put(pubDeviceDto.getImme(),
                        new NetDevice(pubDeviceDto.getDeviceid(), pubDeviceDto.getAdminid(),
                                pubDeviceDto.getPlaceid(), pubDeviceDto.getIsonline()));
            }
        }
    }

    public static void addDeviceName(PubDevice pubDevice) {
        deviceMap.put(pubDevice.getImme(),
                new NetDevice(pubDevice.getDeviceid(), pubDevice.getAdminid(), pubDevice.getPlaceid(),
                        pubDevice.getIsonline()));
    }

    public static void addDeviceName(PubDeviceDto pubDeviceDto) {
        deviceMap.put(pubDeviceDto.getImme(),
                new NetDevice(pubDeviceDto.getDeviceid(), pubDeviceDto.getAdminid(), pubDeviceDto.getPlaceid(),
                        pubDeviceDto.getIsonline()));
    }

    public static Long getDeviceId(String deviceName) {
       /* if(!deviceMap.containsKey(deviceName)) {
            getIntance().addDevice(deviceName);
        }*/

        return deviceMap.get(deviceName) == null ? null : deviceMap.get(deviceName).getDeviceId();
    }

    public static Integer getIsOnline(String deviceName) {
        return deviceMap.get(deviceName) == null ? null : deviceMap.get(deviceName).getIsOnline();
    }


}
